package screen;

import javax.microedition.lcdui.Graphics;

import main.GameCanvas;
import model.Command;
import model.IAction;
import model.Position;
import utilities.GameResource;
import font.BitmapFont;

public abstract class Screen {
	//my member
	public boolean m_bIsOpenScrAnimation = false;
	
	public static final int ITEM_HEIGHT = BitmapFont.m_bmFont.getHeight() + 6;
	
	public Command m_cmdLeft, m_cmdCenter;
	public Command m_cmdRight;
	protected Command m_viewMsgCmd = new Command(GameResource.viewMessage, new IAction() {
        //display message screen
        public void perform() {
            // TODO Auto-generated method stub
            if (MsgScreen.gI().tabs.size() > 0) {
                MsgScreen.gI().switchToMe(GameCanvas.currentScreen);
                m_nMsg = 0;
            } else {
                GameCanvas.startOKDlg(GameResource.noNewMsg);
            }
        }
    });
	
	
	public static Position[] posCmd = new Position[3];
	
	public int m_width;
	protected int m_height;
	protected int m_left, m_top;
	
	public static int cmtoY, cmy, cmdy, cmvy, xL, yL;
	public int m_selected = 0;
	
	public static String m_strMarqueeMsg = null;
	public static int m_iTimeDisplay = 0;
	public static int m_iPosXMsg = 0;
	public static int m_iStrWidth;
	public static int m_nMsg = 0;
    
	public void setSelected(int se){
		m_selected = se;				
	}
	
	static {
		posCmd[0] = new Position(0, GameCanvas.h - 24);
		posCmd[1] = new Position(GameCanvas.hw - 20, GameCanvas.h - 24);
		posCmd[2] = new Position(GameCanvas.w - 40, GameCanvas.h - 24);
		
	}

	public void switchToMe() {
		GameCanvas.clearKeyPressed();
		GameCanvas.currentScreen = this;
		GameCanvas.instance.setFullScreenMode(true);
	}

	//public abstract void showMarqueeMsg();
	public abstract void close();
	public void paintMarqueeMsg(Graphics g){
		BitmapFont.drawBoldFont(g, m_strMarqueeMsg, m_iPosXMsg, 10, 0xffffff, Graphics.LEFT | Graphics.VCENTER);
		
		m_iPosXMsg -= 2;
		if(m_iPosXMsg <= - m_iStrWidth){
			m_iPosXMsg = GameCanvas.w;
			m_iTimeDisplay++;
		}
		if(m_iTimeDisplay > 2){
			m_iTimeDisplay = 0;
			m_strMarqueeMsg = null;
		}
	}
	public void paint(Graphics g) {		
		
		g.translate(-g.getTranslateX(), -g.getTranslateY());
		g.setClip(0, 0, GameCanvas.w, GameCanvas.h);
		
		if(GameCanvas.currentDialog != null && GameCanvas.currentDialog instanceof InputDlg)
			return;
		
		//paint marquee message
		if(m_strMarqueeMsg != null){
			paintMarqueeMsg(g);
			g.setClip(0, 0, GameCanvas.w, GameCanvas.h);
		}
		if (ChatTextField.gI().isShow) {
			PaintPopup.paintCmdBar(g, ChatTextField.gI().left, ChatTextField.gI().center, ChatTextField.gI().right);
		} else {
			if (!GameCanvas.menu.m_showMenu)
				PaintPopup.paintCmdBar(g, m_cmdLeft, m_cmdCenter, m_cmdRight);
		}
	}	
	public abstract void update();

	public void updateKey() {		
		
		if (GameCanvas.isPointerClick) {
			int hKeyBoard = 0;
			for (int i = 0; i < 3; i++) {
				if (GameCanvas.isPointer(posCmd[i].x, posCmd[i].y - hKeyBoard, 40, 22)) {
					switch (i) {
					case 0:
						if (ChatTextField.gI().isShow)
							ChatTextField.gI().left.action.perform();
						else if (m_cmdLeft != null && m_cmdLeft.action != null)
							m_cmdLeft.action.perform();
						break;
					case 1:
						if (ChatTextField.gI().isShow)
							ChatTextField.gI().center.action.perform();
						else if (m_cmdCenter != null && m_cmdCenter.action != null) {
							m_cmdCenter.action.perform();
						}
						break;
					case 2:
						if (ChatTextField.gI().isShow)
							ChatTextField.gI().right.action.perform();
						else if (m_cmdRight != null && m_cmdRight.action != null)
							m_cmdRight.action.perform();
						break;
					}
				}
			}
		}
				
		if (GameCanvas.keyPressed[5]) {
			GameCanvas.keyPressed[5] = false;
			if (ChatTextField.gI().isShow) {
				ChatTextField.gI().center.action.perform();
			}else if (m_cmdCenter != null && m_cmdCenter.action != null) {
				m_cmdCenter.action.perform();
			}
		}
		
		if (GameCanvas.keyPressed[12]) {
			GameCanvas.keyPressed[12] = false;
			if (ChatTextField.gI().isShow)
				ChatTextField.gI().left.action.perform();
			else if (m_cmdLeft != null && m_cmdLeft.action != null)
				m_cmdLeft.action.perform();
		}
		
		//BB: BB can't handle keyPress of soft2
		if (GameCanvas.isBB) {
			if (GameCanvas.keyReleased[13]) {
				GameCanvas.keyReleased[13] = false;
				if (m_cmdRight != null && m_cmdRight.action != null)
					m_cmdRight.action.perform();
					
			}
		} else if (GameCanvas.keyPressed[13]) {
			GameCanvas.keyPressed[13] = false;
			if (ChatTextField.gI().isShow)
				ChatTextField.gI().right.action.perform();
			else if (m_cmdRight != null && m_cmdRight.action != null)
				m_cmdRight.action.perform();
				
		}
	}

	public void keyPress(int keyCode) {
	}	
	
	public void moveCamera() {
		if (xL != 0)
			xL += (-xL >> 1);
		if (xL == -1)
			xL = 0;
		if (yL != 0)
			yL += (-yL >> 1);
		if (yL == -1)
			yL = 0;
		if (cmy != cmtoY) {
			cmvy = (cmtoY - cmy) << 2;
			cmdy += cmvy;
			cmy += cmdy >> 4;
			cmdy = cmdy & 0xf;
		}
	}

	public void onMarqueeMsg(String msg) {
		// TODO Auto-generated method stub
		m_strMarqueeMsg = msg;
		m_iTimeDisplay = 0;
		m_iStrWidth = BitmapFont.m_bmFont.stringWidth(msg);
		m_iPosXMsg = GameCanvas.w;
	}	
}
